package com.helmlabs.grouper.cms.view;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.collections.BeanMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.helmlabs.grouper.cms.model.AbstractModule;
import com.helmlabs.grouper.cms.model.Page;
import com.helmlabs.grouper.cms.model.Region;
import com.helmlabs.grouper.cms.model.RegionalLayout;
import com.helmlabs.grouper.cms.utils.CmsUtil;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreemarkerView extends AbstractView {
	
	private Log log = LogFactory.getLog(getClass());
		
	public OutputStream render(Page page, Map config){
		OutputStream out = new ByteArrayOutputStream();
		OutputStreamWriter writer = new OutputStreamWriter(out);
		Configuration cfg = new Configuration();
		Resource resource = new FileSystemResource("c:\\helmlabs\\source\\grouper-management-suite\\grouper-cms-api\\src\\main\\resources\\themes\\" + CmsUtil.CMS_DEFAULT_THEME + "\\");
		try {
			config.putAll(new BeanMap(page));
			cfg.setDirectoryForTemplateLoading(resource.getFile());
			Template template = cfg.getTemplate("page-head.ftl");
			template.process(config, writer);
			
			if (page.getLayout()!=null){
				config.putAll(new BeanMap(page.getLayout()));
				template = cfg.getTemplate(page.getLayout().getClass().getSimpleName().toLowerCase() + ".ftl");
				log.debug("rendering the layout out of " + page.getLayout());
				if (page.getLayout() instanceof RegionalLayout){
					log.debug("layout is an instance of regional layout");
					RegionalLayout layout = (RegionalLayout)page.getLayout();
					for (Iterator itr = layout.getRegions().keySet().iterator(); itr.hasNext();){
						String key = (String)itr.next();
						Region region = layout.getRegions().get(key);
						config.put(key, render(region, config));
					}
				}
				template.process(config, writer);
			}
			template = cfg.getTemplate("page-foot.ftl");
			template.process(config, writer);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return out;
		
	}
	
	public OutputStream render(Region region, Map config){
		OutputStream out = new ByteArrayOutputStream();
		OutputStreamWriter writer = new OutputStreamWriter(out);
		Configuration cfg = new Configuration();
		Resource resource = new FileSystemResource("c:\\helmlabs\\source\\grouper-management-suite\\grouper-cms-api\\src\\main\\resources\\themes\\" + CmsUtil.CMS_DEFAULT_THEME + "\\");
		try {
			cfg.setDirectoryForTemplateLoading(resource.getFile());
			Template template = null;
			
			for (AbstractModule module:region.getModules()){
				template = cfg.getTemplate("module-head.ftl");
				template.process(config, writer);
				Map moduleConfig = new HashMap();
				moduleConfig.putAll(config);
				moduleConfig.putAll(new BeanMap(module));
				writer.write(render(module, moduleConfig).toString());
				template = cfg.getTemplate("module-foot.ftl");
				template.process(config, writer);				
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return out;
		
	}
	
	public OutputStream render(AbstractModule module, Map config){
		OutputStream out = new ByteArrayOutputStream();
		OutputStreamWriter writer = new OutputStreamWriter(out);
		Configuration cfg = new Configuration();
		Resource resource = new FileSystemResource("c:\\helmlabs\\source\\grouper-management-suite\\grouper-cms-api\\src\\main\\resources\\themes\\" + CmsUtil.CMS_DEFAULT_THEME + "\\");
		try {
			cfg.setDirectoryForTemplateLoading(resource.getFile());
			Template template = cfg.getTemplate(module.getClass().getSimpleName().toLowerCase() + ".ftl");
			template.process(config, writer);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return out;		
	}


}


/**
public static String render(String template, Map model){
Configuration cfg = ne\w Configuration();
Resource resource = new ClassPathResource("/template/main/");
try {
	cfg.setDirectoryForTemplateLoading(resource.getFile());
	cfg.setObjectWrapper(new DefaultObjectWrapper());
	Template temp = cfg.getTemplate(template + ".ftl");
	OutputStream writer = new ByteArrayOutputStream();
	OutputStreamWriter out = new OutputStreamWriter(writer);
	temp.process(model, out);			
	return writer.toString();			
**/